package com.wsc.springbootmultidevdemo.msg;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 发送消息到 MQ
 */
public class Send {
    //队列名称
    private final static  String QUEUE_NAME = "hello";

    public static void main(String[] args) throws IOException, TimeoutException {
        //获得连接及 MQ 通道
        Connection connection = ConnectionUtil.getConnection();

        //创建通道
        Channel channel = connection.createChannel();

        //声明队列 param1:队列名称 param2:是否持久化 param3:是否是独占队列
        //param4:所有消费者客户端连接断开时是否删除队列 param5:队列的其它参数
        //队列声明好了以后就不能更改是否持久化选项
        //rabbitmq 不允许重新定义一个已经存在的 queue
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);

        //消息内容
        String msg = "close connection at once";
        channel.basicPublish("",QUEUE_NAME,null,msg.getBytes());
        System.out.println("sent message :"+msg);

        //关闭连接
        channel.close();
        connection.close();

    }
}
